package cn.shop.ylp.controll;

import cn.shop.ylp.Product;
import cn.shop.ylp.ProductMapper;
import cn.shop.ylp.error.CommonReturnType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author yulianpeng
 * @email network_u@163.com
 * Date 2020/9/10
 */
@Controller
@RequestMapping("/goods")
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class Goods extends BaseController{
    @Autowired
    private ProductMapper productMapper;
    @RequestMapping(value = "/all",method = {RequestMethod.GET})
    @ResponseBody
    public CommonReturnType all(){
        ArrayList<Product> products = productMapper.all();
        TreeSet<String> tSet = new TreeSet<>();
        HashMap<String,Object> tCategory= new HashMap<>();
        for (int i=0;i<products.size();i++){
            tSet.add(products.get(i).getCategory());
        }
        ArrayList<String> list = new ArrayList<>();
        Iterator<String> tSetTor=tSet.iterator();
        while (tSetTor.hasNext()){
            String tLastCategory =tSetTor.next();
            ArrayList<Product> tPro = new ArrayList<Product>();
            for (int i=0;i<products.size();i++){
                if(tLastCategory.equals(products.get(i).getCategory())) {
                    tPro.add(products.get(i));
                }
            }
            list.add(tLastCategory);
            tCategory.put(tLastCategory,tPro);

        }
        tCategory.put("list",list);
        CommonReturnType tR = CommonReturnType.create(tCategory,"success");
        return tR;
    }
    @RequestMapping(value = "/detail",method = {RequestMethod.GET})
    @ResponseBody
    public CommonReturnType detail(@RequestParam(name = "goodid") String goodid){
        Product pro=productMapper.selectByPrimaryKey(Integer.parseInt(goodid));
        return CommonReturnType.create(pro);
    }
}